import { songOfSongId } from "api/index";

export default class Song {
  constructor({id, singerId, name, introduction, pic, url,rankSongId}) {
    this.id = id
    this.singerId = singerId
    this.name = name
    this.introduction = introduction
    this.pic = pic
    this.url = url
    this.rankSongId = rankSongId
  }

  getLyric() {
    if (this.lyric) {
      return Promise.resolve(this.lyric)
    }
    return new Promise((resolve, reject) => {
      songOfSongId(this.id).then(res => {
        if (res) {
          this.lyric = res.lyric;
          resolve(this.lyric)
        }else {
          reject('no lyric')
        }
      })
    })
  }
}

export function createSong(song) {
  return new Song({
    id: song.id,
    singerId: song.singerId,
    name: song.name,
    introduction: song.introduction,
    pic: `http://127.0.0.1:8888${song.pic}`,
    url: `http://127.0.0.1:8888${song.url}`,
    rankSongId: song.rankSongId
  })
}

